<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>go-redis文档 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/91.b8d1b7ea.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go工具中间件</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/middleware/go-coding-standard.html" class="sidebar-link">Golang语言编码规范</a></li><li><a href="/go/middleware/go-get命令详解.html" class="sidebar-link">go get命令详解</a></li><li><a href="/go/middleware/go-mysql.html" class="sidebar-link">Go语言操作Mysql</a></li><li><a href="/go/middleware/go-redis.html" class="active sidebar-link">go-redis文档</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_1-redis优势" class="sidebar-link">1 Redis优势</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_2-安装" class="sidebar-link">2 安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_2-1-安装依赖包" class="sidebar-link">2.1 安装依赖包</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_3-go连接redis" class="sidebar-link">3 Go连接Redis</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_3-1-redis-options参数详解" class="sidebar-link">3.1 redis.Options参数详解</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-基本键值操作" class="sidebar-link">4 基本键值操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-1-set-get示例" class="sidebar-link">4.1 Set &amp; Get示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-2-getset-setnx示例" class="sidebar-link">4.2 GetSet &amp; SetNX示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-3-mget-mset示例" class="sidebar-link">4.3 MGet &amp; MSet示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-4-自增自减操作" class="sidebar-link">4.4 自增自减操作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_4-5-del-expire-append示例" class="sidebar-link">4.5 Del &amp; Expire &amp; Append示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-list操作" class="sidebar-link">5 list操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-1-lpush-lpushx-示例" class="sidebar-link">5.1 LPush &amp; LPushX 示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-2-lrange-llen示例" class="sidebar-link">5.2 LRange &amp; LLen示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-3-ltrim-lindex-示例" class="sidebar-link">5.3 LTrim &amp; LIndex 示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-4-lset-linsert示例" class="sidebar-link">5.4 LSet &amp; LInsert示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_5-5-lpop-lrem示例" class="sidebar-link">5.5 LPop &amp; LRem示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-集合set操作" class="sidebar-link">6 集合set操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-1-sadd-scard示例" class="sidebar-link">6.1 SAdd &amp; SCard示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-2-sismember-smembers示例" class="sidebar-link">6.2 SIsMember &amp; SMembers示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-3-并集-交集-差集" class="sidebar-link">6.3 并集&amp;交集&amp;差集</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-4-集合删除操作" class="sidebar-link">6.4 集合删除操作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-5-集合随机数操作" class="sidebar-link">6.5 集合随机数操作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_6-6-smembersmap-smove示例" class="sidebar-link">6.6 SMembersMap &amp; SMove示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-hash数据操作" class="sidebar-link">7 hash数据操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-1-使用场景举例" class="sidebar-link">7.1 使用场景举例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-2-hash数据常用函数汇总" class="sidebar-link">7.2 hash数据常用函数汇总</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-3-hget-hset等操作" class="sidebar-link">7.3 HGet &amp; HSet等操作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-4-hincrby-hincrbyfloat" class="sidebar-link">7.4 HIncrBy &amp; HIncrByFloat</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-5-hkeys-hlen示例" class="sidebar-link">7.5 HKeys &amp; HLen示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_7-6-hdel-hexists示例" class="sidebar-link">7.6 HDel &amp; HExists示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-有序集合-sorted-set" class="sidebar-link">8 有序集合(sorted set)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-1-有序集合的常用方法" class="sidebar-link">8.1 有序集合的常用方法</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-2-zadd-zincr示例" class="sidebar-link">8.2 ZAdd &amp; ZIncr示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-3-zcount-zcard示例" class="sidebar-link">8.3 ZCount &amp; ZCard示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-4-zrange等相关操作" class="sidebar-link">8.4 ZRange等相关操作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-5-zrank-zscore示例" class="sidebar-link">8.5 ZRank &amp; ZScore示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_8-6-删除集合元素" class="sidebar-link">8.6 删除集合元素</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_9-事务处理" class="sidebar-link">9 事务处理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_9-1-redis事务常用函数" class="sidebar-link">9.1 redis事务常用函数</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_9-2-txpipeline示例" class="sidebar-link">9.2 TxPipeline示例</a></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_9-3-watch" class="sidebar-link">9.3 watch</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/go-redis.html#_10-更多资料参考" class="sidebar-link">10 更多资料参考</a></li></ul></li><li><a href="/go/middleware/go-swagger.html" class="sidebar-link">go-swagger安装及使用</a></li><li><a href="/go/middleware/go-viper.html" class="sidebar-link">viper操作文档</a></li><li><a href="/go/middleware/Goland的自定义快捷键配置.html" class="sidebar-link">配置Goland的自定义快捷键</a></li><li><a href="/go/middleware/Golang的安装配置golint.html" class="sidebar-link">Golang的安装配置golint</a></li><li><a href="/go/middleware/goproxy.html" class="sidebar-link">GoLand配置GOPROXY代理</a></li><li><a href="/go/middleware/gorm.html" class="sidebar-link">GORM基本操作</a></li><li><a href="/go/middleware/json-iterator.html" class="sidebar-link">json-iterator使用</a></li><li><a href="/go/middleware/package.html" class="sidebar-link">Go语言包和包管理</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go-redis文档"><a href="#go-redis文档" class="header-anchor">#</a> go-redis文档</h1> <h2 id="_1-redis优势"><a href="#_1-redis优势" class="header-anchor">#</a> 1 Redis优势</h2> <blockquote><p>更多Redis学习请参考本网站<a href="/sql/nosql/Redis简介.html">Redis教程</a></p></blockquote> <div class="custom-block tip"><p class="custom-block-title">Redis 优势</p> <ul><li>性能极高 –- <code>Redis</code>能读的速度是<code>110000次/s</code>,写的速度是<code>81000次/s</code> 。</li> <li>丰富的数据类型 –- <code>Redis</code>支持二进制案例的 <code>Strings,Lists,Hashes,Sets及Ordered Sets</code>数据类型操作。</li> <li>原子--<code>Redis</code>的所有操作都是原子性的，同时<code>Redis</code>还支持对几个操作全并后的原子性执行。</li> <li>丰富的特性 – Redis还支持<code>publish/subscribe</code>,通知,<code>key</code>过期等等特性。</li></ul></div> <h2 id="_2-安装"><a href="#_2-安装" class="header-anchor">#</a> 2 安装</h2> <blockquote><p><code>Redis</code>的安装很简单,我这里测试直接用的是<code>windows</code>的版本。如何windows安装请自行百度。<a href="/blog/CentOS/Centos安装Redis.html">linux安装Redis教程</a></p></blockquote> <h3 id="_2-1-安装依赖包"><a href="#_2-1-安装依赖包" class="header-anchor">#</a> 2.1 安装依赖包</h3> <blockquote><p><code>golang</code>客户端，用的是<code>go-redis</code>,直接使用go get包下载下来导入即可。<a href="https://godoc.org/github.com/go-redis/redis" target="_blank" rel="noopener noreferrer">go-redis文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> get <span class="token operator">-</span>u github<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token keyword">go</span><span class="token operator">-</span>redis<span class="token operator">/</span>redis
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_3-go连接redis"><a href="#_3-go连接redis" class="header-anchor">#</a> 3 Go连接Redis</h2> <blockquote><p><code>go-redis</code>包自带了连接池，会自动维护<code>redis</code>连接，因此创建一次<code>client</code>即可，不要查询一次<code>redis</code>就关闭<code>client</code>。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/go-redis/redis&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// 声明一个全局的redisDb变量</span>
<span class="token keyword">var</span> redisDb <span class="token operator">*</span>redis<span class="token punctuation">.</span>Client

<span class="token comment">// 根据redis配置初始化一个客户端</span>
<span class="token keyword">func</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	redisDb <span class="token operator">=</span> redis<span class="token punctuation">.</span><span class="token function">NewClient</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>redis<span class="token punctuation">.</span>Options<span class="token punctuation">{</span>
		Addr<span class="token punctuation">:</span>     <span class="token string">&quot;localhost:6379&quot;</span><span class="token punctuation">,</span> <span class="token comment">// redis地址</span>
		Password<span class="token punctuation">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>               <span class="token comment">// redis密码，没有则留空</span>
		DB<span class="token punctuation">:</span>       <span class="token number">0</span><span class="token punctuation">,</span>                <span class="token comment">// 默认数据库，默认是0</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//通过 *redis.Client.Ping() 来检查是否成功连接到了redis服务器</span>
	<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Ping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Redis连接成功&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><h3 id="_3-1-redis-options参数详解"><a href="#_3-1-redis-options参数详解" class="header-anchor">#</a> 3.1 redis.Options参数详解</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Options <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	<span class="token comment">// 网络类型 tcp 或者 unix.</span>
	<span class="token comment">// 默认是 tcp.</span>
	Network <span class="token builtin">string</span>
	<span class="token comment">// redis地址，格式 host:port</span>
	Addr <span class="token builtin">string</span>

	<span class="token comment">// 新建一个redis连接的时候，会回调这个函数</span>
	OnConnect <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token operator">*</span>Conn<span class="token punctuation">)</span> <span class="token builtin">error</span>

	<span class="token comment">// redis密码，redis server没有设置可以为空。</span>
	Password <span class="token builtin">string</span>
	
	<span class="token comment">// redis数据库，序号从0开始，默认是0，可以不用设置</span>
	DB <span class="token builtin">int</span>

	<span class="token comment">// redis操作失败最大重试次数，默认不重试。</span>
	MaxRetries <span class="token builtin">int</span>
	
	<span class="token comment">// 最小重试时间间隔.</span>
	<span class="token comment">// 默认是 8ms ; -1 表示关闭.</span>
	MinRetryBackoff time<span class="token punctuation">.</span>Duration
	
	<span class="token comment">// 最大重试时间间隔</span>
	<span class="token comment">// 默认是 512ms; -1 表示关闭.</span>
	MaxRetryBackoff time<span class="token punctuation">.</span>Duration

	<span class="token comment">// redis连接超时时间.</span>
	<span class="token comment">// 默认是 5 秒.</span>
	DialTimeout time<span class="token punctuation">.</span>Duration
	
	<span class="token comment">// socket读取超时时间</span>
	<span class="token comment">// 默认 3 秒.</span>
	ReadTimeout time<span class="token punctuation">.</span>Duration
	
	<span class="token comment">// socket写超时时间</span>
	WriteTimeout time<span class="token punctuation">.</span>Duration

	<span class="token comment">// redis连接池的最大连接数.</span>
	<span class="token comment">// 默认连接池大小等于 cpu个数 * 10</span>
	PoolSize <span class="token builtin">int</span>
	
	<span class="token comment">// redis连接池最小空闲连接数.</span>
	MinIdleConns <span class="token builtin">int</span>
	<span class="token comment">// redis连接最大的存活时间，默认不会关闭过时的连接.</span>
	MaxConnAge time<span class="token punctuation">.</span>Duration
	
	<span class="token comment">// 当你从redis连接池获取一个连接之后，连接池最多等待这个拿出去的连接多长时间。</span>
	<span class="token comment">// 默认是等待 ReadTimeout + 1 秒.</span>
	PoolTimeout time<span class="token punctuation">.</span>Duration
	<span class="token comment">// redis连接池多久会关闭一个空闲连接.</span>
	<span class="token comment">// 默认是 5 分钟. -1 则表示关闭这个配置项</span>
	IdleTimeout time<span class="token punctuation">.</span>Duration
	<span class="token comment">// 多长时间检测一下，空闲连接</span>
	<span class="token comment">// 默认是 1 分钟. -1 表示关闭空闲连接检测</span>
	IdleCheckFrequency time<span class="token punctuation">.</span>Duration

	<span class="token comment">// 只读设置，如果设置为true， redis只能查询缓存不能更新。</span>
	readOnly <span class="token builtin">bool</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br></div></div><h2 id="_4-基本键值操作"><a href="#_4-基本键值操作" class="header-anchor">#</a> 4 基本键值操作</h2> <blockquote><p><code>redis</code>基本的<code>key/value</code>操作,指的是针对<code>value</code>值的类型为字符串或者数字类型的读写操作。<code>go-redis</code>常用函数如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Cmdable <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">//给数据库中名称为key的string赋予值value,并设置失效时间，0为永久有效</span>
    <span class="token function">Set</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> expiration time<span class="token punctuation">.</span>Duration<span class="token punctuation">)</span> <span class="token operator">*</span>StatusCmd
    <span class="token comment">//查询数据库中名称为key的value值</span>
    <span class="token function">Get</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//设置一个key的值，并返回这个key的旧值</span>
    <span class="token function">GetSet</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//如果key不存在，则设置这个key的值,并设置key的失效时间。如果key存在，则设置不生效</span>
    <span class="token function">SetNX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> expiration time<span class="token punctuation">.</span>Duration<span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
    <span class="token comment">//批量查询key的值。比如redisDb.MGet(&quot;name1&quot;,&quot;name2&quot;,&quot;name3&quot;)</span>
    <span class="token function">MGet</span><span class="token punctuation">(</span>keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>SliceCmd
    <span class="token comment">//批量设置key的值。redisDb.MSet(&quot;key1&quot;, &quot;value1&quot;, &quot;key2&quot;, &quot;value2&quot;, &quot;key3&quot;, &quot;value3&quot;)</span>
    <span class="token function">MSet</span><span class="token punctuation">(</span>pairs <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>StatusCmd
    <span class="token comment">//Incr函数每次加一,key对应的值必须是整数或nil</span>
    <span class="token comment">//否则会报错incr key1: ERR value is not an integer or out of range</span>
    <span class="token function">Incr</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">// IncrBy函数,可以指定每次递增多少,key对应的值必须是整数或nil</span>
    <span class="token function">IncrBy</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">// IncrByFloat函数,可以指定每次递增多少，跟IncrBy的区别是累加的是浮点数</span>
    <span class="token function">IncrByFloat</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token builtin">float64</span><span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
    <span class="token comment">// Decr函数每次减一,key对应的值必须是整数或nil.否则会报错</span>
    <span class="token function">Decr</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//DecrBy,可以指定每次递减多少,key对应的值必须是整数或nil</span>
	<span class="token function">DecrBy</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> decrement <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//删除key操作,支持批量删除	redisDb.Del(&quot;key1&quot;,&quot;key2&quot;,&quot;key3&quot;)</span>
    <span class="token function">Del</span><span class="token punctuation">(</span>keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//设置key的过期时间,单位秒</span>
    <span class="token function">Expire</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> expiration time<span class="token punctuation">.</span>Duration<span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
    <span class="token comment">//给数据库中名称为key的string值追加value</span>
    <span class="token function">Append</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h3 id="_4-1-set-get示例"><a href="#_4-1-set-get示例" class="header-anchor">#</a> 4.1 Set &amp; Get示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 第三个参数代表key的过期时间，0代表不会过期。</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;name1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">var</span> val <span class="token builtin">string</span>
	<span class="token comment">// Result函数返回两个值，第一个是key的值，第二个是错误信息</span>
	val<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;name1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">// 判断查询是否出错</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;name1的值：&quot;</span><span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token comment">//name1的值：zhangsan</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_4-2-getset-setnx示例"><a href="#_4-2-getset-setnx示例" class="header-anchor">#</a> 4.2 GetSet &amp; SetNX示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 第三个参数代表key的过期时间，0代表不会过期。</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;name1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">var</span> oldVal <span class="token builtin">string</span>
	<span class="token comment">// Result函数返回两个值，第一个是key的值，第二个是错误信息</span>
	oldVal<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">GetSet</span><span class="token punctuation">(</span><span class="token string">&quot;name1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;new_zhangsan&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 打印key的旧值</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;name1：&quot;</span><span class="token punctuation">,</span> oldVal<span class="token punctuation">)</span>

	<span class="token comment">//如果key不存在，则设置这个key的值,并设置key的失效时间。如果key存在，则设置不生效</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SetNX</span><span class="token punctuation">(</span><span class="token string">&quot;name2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h3 id="_4-3-mget-mset示例"><a href="#_4-3-mget-mset示例" class="header-anchor">#</a> 4.3 MGet &amp; MSet示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//批量设置key1对应的值为value1，key2对应的值为value2，key3对应的值为value3，</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">MSet</span><span class="token punctuation">(</span><span class="token string">&quot;key1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;value1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;value2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key3&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;value3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// MGet函数可以传入任意个key，一次性返回多个值。</span>
	<span class="token comment">// 这里Result返回两个值，第一个值是一个数组，第二个值是错误信息</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">MGet</span><span class="token punctuation">(</span><span class="token string">&quot;key1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//[value1 value2 value3]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_4-4-自增自减操作"><a href="#_4-4-自增自减操作" class="header-anchor">#</a> 4.4 自增自减操作</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//设置一个age测试自增、自减</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;20&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Incr</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span>      <span class="token comment">// 自增</span>
	redisDb<span class="token punctuation">.</span><span class="token function">IncrBy</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">//+5</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Decr</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span>      <span class="token comment">// 自减</span>
	redisDb<span class="token punctuation">.</span><span class="token function">DecrBy</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">//-3 此时age的值是22</span>

	<span class="token keyword">var</span> val <span class="token builtin">string</span>
	val<span class="token punctuation">,</span> err<span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;age=&quot;</span><span class="token punctuation">,</span>val<span class="token punctuation">)</span> <span class="token comment">//22</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_4-5-del-expire-append示例"><a href="#_4-5-del-expire-append示例" class="header-anchor">#</a> 4.5 Del &amp; Expire &amp; Append示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//设置一个name对应的值是hello</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Append</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;China&quot;</span><span class="token punctuation">)</span>
	val<span class="token punctuation">,</span>err<span class="token operator">:=</span>redisDb<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span> <span class="token comment">//helloChina</span>
	
	<span class="token comment">//设置key的过期时间为5秒</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Expire</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
	<span class="token comment">//批量删除</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Del</span><span class="token punctuation">(</span><span class="token string">&quot;name1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;name2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="_5-list操作"><a href="#_5-list操作" class="header-anchor">#</a> 5 list操作</h2> <blockquote><p><code>Redis</code>列表(<code>list</code>)是简单的字符串列表,列表是有序的,列表中的元素可以重复.可以添加一个元素到列表的头部(左边)或者尾部(右边)常用操作方法如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Cmdable <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">//从列表左边插入数据,list不存在则新建一个继续插入数据</span>
	<span class="token function">LPush</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> values <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//跟LPush的区别是，仅当列表存在的时候才插入数据</span>
	<span class="token function">LPushX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//返回名称为 key 的 list 中 start 至 end 之间的元素</span>
    <span class="token comment">//返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	<span class="token function">LRange</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//返回列表的长度大小</span>
	<span class="token function">LLen</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//截取名称为key的list的数据，list的数据为截取后的值</span>
	<span class="token function">LTrim</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StatusCmd
    <span class="token comment">//根据索引坐标，查询列表中的数据</span>
    <span class="token function">LIndex</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> index <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//给名称为key的list中index位置的元素赋值</span>
	<span class="token function">LSet</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> index <span class="token builtin">int64</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>StatusCmd
    <span class="token comment">//在指定位置插入数据。op为&quot;after或者before&quot;</span>
	<span class="token function">LInsert</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> op <span class="token builtin">string</span><span class="token punctuation">,</span> pivot<span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//在指定位置前面插入数据</span>
	<span class="token function">LInsertBefore</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> pivot<span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//在指定位置后面插入数据</span>
	<span class="token function">LInsertAfter</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> pivot<span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//从列表左边删除第一个数据，并返回删除的数据</span>
	<span class="token function">LPop</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//删除列表中的数据。删除count个key的list中值为value 的元素。</span>
	<span class="token function">LRem</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> count <span class="token builtin">int64</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
   <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_5-1-lpush-lpushx-示例"><a href="#_5-1-lpush-lpushx-示例" class="header-anchor">#</a> 5.1 LPush &amp; LPushX 示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//仅当列表存在的时候才插入数据,此时列表不存在，无法插入</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LPushX</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">//此时列表不存在，依然可以插入</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;jack&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">//此时列表存在的时候才能插入数据</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LPushX</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">// LPush支持一次插入任意个数据</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lily&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;lisi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//注意列表是有序的，输出结果是[lisi zhangsan lilei lily tom jack]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_5-2-lrange-llen示例"><a href="#_5-2-lrange-llen示例" class="header-anchor">#</a> 5.2 LRange &amp; LLen示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// LPush支持一次插入任意个数据</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lily&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;lisi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 返回从[0,2]位置之间的数据，意思就是返回3个数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//注意列表是有序的，输出结果是[lisi zhangsan lilei]</span>

	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//返回list集合中的长度</span>
	studentLen<span class="token punctuation">,</span> err <span class="token operator">:=</span>redisDb<span class="token punctuation">.</span><span class="token function">LLen</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;student集合的长度为:&quot;</span><span class="token punctuation">,</span>studentLen<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h3 id="_5-3-ltrim-lindex-示例"><a href="#_5-3-ltrim-lindex-示例" class="header-anchor">#</a> 5.3 LTrim &amp; LIndex 示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// LPush支持一次插入任意个数据</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lily&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//注意列表是有序的，输出结果是[tom lisi zhangsan lilei lily]</span>

	<span class="token comment">// 列表索引从0开始计算，这里返回第3个元素</span>
	index<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LIndex</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token comment">//zhangsan</span>

	<span class="token comment">//截取名称为key的list,并把截取后的值赋值给studentList</span>
	val<span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LTrim</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span> <span class="token comment">//ltrim studentList 0 3: OK</span>

	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err<span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//[tom lisi zhangsan lilei]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="_5-4-lset-linsert示例"><a href="#_5-4-lset-linsert示例" class="header-anchor">#</a> 5.4 LSet &amp; LInsert示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// LPush支持一次插入任意个数据</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lily&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//给名称为key的list中index位置的元素赋值，把原来的数据覆盖</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LSet</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">&quot;beer&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//注意列表是[tom lisi beer lilei lily]</span>

	<span class="token comment">//在list列表studentList中值为lilei前面添加元素hello</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LInsert</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;before&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//redisDb.LInsertBefore(&quot;studentList&quot;,&quot;lilei&quot;,&quot;hello&quot;) 执行效果同22行</span>

	<span class="token comment">//在list列表studentList中值为tom后面添加元素world</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LInsert</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;after&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;world&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//redisDb.LInsertAfter(&quot;studentList&quot;,&quot;tom&quot;,&quot;world&quot;) 执行效果同26行</span>

	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>vals<span class="token punctuation">)</span> <span class="token comment">//[tom world lisi beer hello lilei lily]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="_5-5-lpop-lrem示例"><a href="#_5-5-lpop-lrem示例" class="header-anchor">#</a> 5.5 LPop &amp; LRem示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// LPush支持一次插入任意个数据</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LPush</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lily&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lilei&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;tom&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;laowang&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;还未进行任何删除时列表中的值为:&quot;</span><span class="token punctuation">,</span> vals<span class="token punctuation">)</span> <span class="token comment">//[laowang lisi tom lisi zhangsan lilei lily]</span>
	<span class="token comment">//从列表左边删除第一个数据，并返回删除的数据</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LPop</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 返回从0开始到-1位置之间的数据，意思就是返回全部数据</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;进行了LPop删除操作后列表的值：&quot;</span><span class="token punctuation">,</span> vals<span class="token punctuation">)</span> <span class="token comment">//[lisi tom lisi zhangsan lilei lily]</span>

	<span class="token comment">//删除列表中的数据。删除count个key的list中值为value 的元素。如果出现重复元素，仅删除1次，也就是删除第一个</span>
	redisDb<span class="token punctuation">.</span><span class="token function">LRem</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">)</span>
	vals<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">LRange</span><span class="token punctuation">(</span><span class="token string">&quot;studentList&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;进行了LRem删除操作后列表的值：&quot;</span><span class="token punctuation">,</span> vals<span class="token punctuation">)</span> <span class="token comment">// [tom zhangsan lilei lily]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h2 id="_6-集合set操作"><a href="#_6-集合set操作" class="header-anchor">#</a> 6 集合set操作</h2> <blockquote><p><code>redi</code>s的<code>set类型</code>（集合）是<code>string</code>类型数值的无序集合，并且集合元素唯一。常用函数如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Cmdable <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">//向名称为key的set中添加元素member</span>
    <span class="token function">SAdd</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//获取集合set元素个数</span>
	<span class="token function">SCard</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//判断元素member是否在集合set中</span>
	<span class="token function">SIsMember</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> member <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
    <span class="token comment">//返回名称为 key 的 set 的所有元素</span>
	<span class="token function">SMembers</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//求差集</span>
	<span class="token function">SDiff</span><span class="token punctuation">(</span>keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//求差集并将差集保存到 destination 的集合</span>
	<span class="token function">SDiffStore</span><span class="token punctuation">(</span>destination <span class="token builtin">string</span><span class="token punctuation">,</span> keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//求交集</span>
	<span class="token function">SInter</span><span class="token punctuation">(</span>keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//求交集并将交集保存到 destination 的集合</span>
	<span class="token function">SInterStore</span><span class="token punctuation">(</span>destination <span class="token builtin">string</span><span class="token punctuation">,</span> keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//求并集</span>
	<span class="token function">SUnion</span><span class="token punctuation">(</span>keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//求并集并将并集保存到 destination 的集合</span>
	<span class="token function">SUnionStore</span><span class="token punctuation">(</span>destination <span class="token builtin">string</span><span class="token punctuation">,</span> keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//随机返回集合中的一个元素，并且删除这个元素</span>
	<span class="token function">SPop</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">// 随机返回集合中的count个元素，并且删除这些元素</span>
	<span class="token function">SPopN</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> count <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//删除名称为 key 的 set 中的元素 member,并返回删除的元素个数</span>
	<span class="token function">SRem</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//随机返回名称为 key 的 set 的一个元素</span>
	<span class="token function">SRandMember</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//随机返回名称为 key 的 set 的count个元素</span>
	<span class="token function">SRandMemberN</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> count <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//把集合里的元素转换成map的key</span>
	<span class="token function">SMembersMap</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringStructMapCmd
    <span class="token comment">//移动集合source中的一个member元素到集合destination中去</span>
	<span class="token function">SMove</span><span class="token punctuation">(</span>source<span class="token punctuation">,</span> destination <span class="token builtin">string</span><span class="token punctuation">,</span> member <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><h3 id="_6-1-sadd-scard示例"><a href="#_6-1-sadd-scard示例" class="header-anchor">#</a> 6.1 SAdd &amp; SCard示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 添加100到集合中</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 将100,200,300批量添加到集合中 集合的元素不能重复</span>
	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span>
	<span class="token comment">//获取集合中元素的个数</span>
	size<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SCard</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>size<span class="token punctuation">)</span> <span class="token comment">//3</span>
	<span class="token comment">//返回名称为集合中的所有元素</span>
	es<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>es<span class="token punctuation">)</span> <span class="token comment">//[100 200 300]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_6-2-sismember-smembers示例"><a href="#_6-2-sismember-smembers示例" class="header-anchor">#</a> 6.2 SIsMember &amp; SMembers示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 添加100到集合中</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 将100,200,300批量添加到集合中 集合的元素不能重复</span>
	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span>

	<span class="token comment">//返回名称为集合中的所有元素</span>
	es<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>es<span class="token punctuation">)</span> <span class="token comment">//[100 200 300]</span>

	<span class="token comment">//此处flag1=true</span>
	flag1<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SIsMember</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;200&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> flag1 <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;集合stuSet中包含指定元素200&quot;</span><span class="token punctuation">)</span> <span class="token comment">//输出</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;集合stuSet不包含元素200&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//此处flag2=false</span>
	flag2<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SIsMember</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;400&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> flag2 <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;集合stuSet中包含指定元素400&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;集合stuSet不包含元素400&quot;</span><span class="token punctuation">)</span> <span class="token comment">//输出</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_6-3-并集-交集-差集"><a href="#_6-3-并集-交集-差集" class="header-anchor">#</a> 6.3 并集&amp;交集&amp;差集</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Obama&quot;</span><span class="token punctuation">)</span>     <span class="token comment">// 向 blacklist 中添加元素</span>
	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Hillary&quot;</span><span class="token punctuation">)</span>   <span class="token comment">// 再次添加</span>
	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;the Elder&quot;</span><span class="token punctuation">)</span> <span class="token comment">// 添加新元素</span>

	redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;whitelist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;the Elder&quot;</span><span class="token punctuation">)</span> <span class="token comment">// 向 whitelist 添加元素</span>

	<span class="token comment">// 求交集, 即既在黑名单中, 又在白名单中的元素</span>
	names<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SInter</span><span class="token punctuation">(</span><span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;whitelist&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 获取到的元素是 &quot;the Elder&quot;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;交集结果是: &quot;</span><span class="token punctuation">,</span> names<span class="token punctuation">)</span> <span class="token comment">// [the Elder]</span>

	<span class="token comment">//求交集并将交集保存到 destSet 的集合</span>
	res<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SInterStore</span><span class="token punctuation">(</span><span class="token string">&quot;destSet&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;whitelist&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
	<span class="token comment">//获取交集的值[the Elder]</span>
	destStr<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;destSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>destStr<span class="token punctuation">)</span> <span class="token comment">//[the Elder]</span>

	<span class="token comment">// 求差集</span>
	diffStr<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SDiff</span><span class="token punctuation">(</span><span class="token string">&quot;blacklist&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;whitelist&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;差集结果是: &quot;</span><span class="token punctuation">,</span> diffStr<span class="token punctuation">)</span> <span class="token comment">//[Hillary Obama]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><h3 id="_6-4-集合删除操作"><a href="#_6-4-集合删除操作" class="header-anchor">#</a> 6.4 集合删除操作</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 将100,200,300批量添加到集合中 集合的元素不能重复</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	res1<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;res1=&quot;</span><span class="token punctuation">,</span> res1<span class="token punctuation">)</span> <span class="token comment">//res1= [100 200 300 400 500 600]</span>

	<span class="token comment">//随机返回集合中的一个元素，并且删除这个元素,这里删除的是400</span>
	member1<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SPop</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>member1<span class="token punctuation">)</span> <span class="token comment">//400</span>

	res2<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;res2=&quot;</span><span class="token punctuation">,</span> res2<span class="token punctuation">)</span> <span class="token comment">//res2= [100 200 300 500 600]</span>

	<span class="token comment">// 随机返回集合中的4个元素，并且删除这些元素</span>
	member2<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SPopN</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>member2<span class="token punctuation">)</span> <span class="token comment">//[]</span>

	res3<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;res3=&quot;</span><span class="token punctuation">,</span> res3<span class="token punctuation">)</span> <span class="token comment">// [100 200 300 500 600]</span>

	<span class="token comment">//删除集合stuSet名称为300,400的元素,并返回删除的元素个数</span>
	member3<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SRem</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>member3<span class="token punctuation">)</span> <span class="token comment">//2</span>
	res4<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;res4=&quot;</span><span class="token punctuation">,</span> res4<span class="token punctuation">)</span> <span class="token comment">//res4= [100 200 300]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><h3 id="_6-5-集合随机数操作"><a href="#_6-5-集合随机数操作" class="header-anchor">#</a> 6.5 集合随机数操作</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 将100,200,300批量添加到集合中 集合的元素不能重复</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//随机返回集合stuSet中的一个元素</span>
	member1<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SRandMember</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>member1<span class="token punctuation">)</span> <span class="token comment">//600</span>
	<span class="token comment">//随机返回集合stuSet中的3个元素</span>
	member2<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SRandMemberN</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>member2<span class="token punctuation">)</span> <span class="token comment">//[600 400 500]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="_6-6-smembersmap-smove示例"><a href="#_6-6-smembersmap-smove示例" class="header-anchor">#</a> 6.6 SMembersMap &amp; SMove示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 将100,200,300批量添加到集合中 集合的元素不能重复</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">SAdd</span><span class="token punctuation">(</span><span class="token string">&quot;resSet&quot;</span><span class="token punctuation">,</span> <span class="token number">900</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//把集合里的元素转换成map的key</span>
	map1<span class="token punctuation">,</span> err<span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembersMap</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>map1<span class="token punctuation">)</span> <span class="token comment">//map[100:{} 200:{} 300:{} 400:{} 500:{} 600:{}]</span>

	<span class="token comment">//移动集合stuSet中的一个200元素到集合resSet中去</span>
	ok<span class="token punctuation">,</span> err<span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMove</span><span class="token punctuation">(</span><span class="token string">&quot;stuSet&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;resSet&quot;</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> ok<span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;移动数据成功&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//返回resSet集合中的所有元素</span>
	resSetStr<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">SMembers</span><span class="token punctuation">(</span><span class="token string">&quot;resSet&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>resSetStr<span class="token punctuation">)</span> <span class="token comment">//[200 900]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="_7-hash数据操作"><a href="#_7-hash数据操作" class="header-anchor">#</a> 7 hash数据操作</h2> <blockquote><p>如果你希望<code>key/value</code>的值也能作为<code>hash</code>结构进行操作，可以选择<code>redis hash</code>类型。</p></blockquote> <h3 id="_7-1-使用场景举例"><a href="#_7-1-使用场景举例" class="header-anchor">#</a> 7.1 使用场景举例</h3> <blockquote><p>如果我们希望缓存一条用户信息(包括用户id、用户名、email字段)，希望能够做到局部读写用户信息（例如：读写用户名），也能够读取整条用户信息，那么hash类型就支持这些操作。
<code>Redis hash</code>操作主要有如下2-3个元素组成</p></blockquote> <ul><li><code>key</code> - <code>redis key</code>唯一标识</li> <li><code>field</code> - <code>hash</code>数据的字段名</li> <li><code>value</code> - 值,有些操作不需要值</li></ul> <h3 id="_7-2-hash数据常用函数汇总"><a href="#_7-2-hash数据常用函数汇总" class="header-anchor">#</a> 7.2 hash数据常用函数汇总</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Cmdable <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">//根据key和字段名，删除hash字段，支持批量删除hash字段</span>
    <span class="token function">HDel</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> fields <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//检测hash字段名是否存在。</span>
	<span class="token function">HExists</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
    <span class="token comment">//根据key和field字段，查询field字段的值</span>
	<span class="token function">HGet</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringCmd
    <span class="token comment">//根据key查询所有字段和值</span>
	<span class="token function">HGetAll</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringStringMapCmd
    <span class="token comment">//根据key和field字段，累加数值。</span>
	<span class="token function">HIncrBy</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">,</span> incr <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//根据key和field字段，累加数值。</span>
	<span class="token function">HIncrByFloat</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">,</span> incr <span class="token builtin">float64</span><span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
    <span class="token comment">//根据key返回所有字段名</span>
	<span class="token function">HKeys</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//根据key，查询hash的字段数量</span>
	<span class="token function">HLen</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//根据key和多个字段名，批量查询多个hash字段值</span>
	<span class="token function">HMGet</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> fields <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>SliceCmd
    <span class="token comment">//根据key和多个字段名和字段值，批量设置hash字段值</span>
	<span class="token function">HMSet</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> fields <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>StatusCmd
    <span class="token comment">//根据key和field字段设置，field字段的值</span>
	<span class="token function">HSet</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
    <span class="token comment">//根据key和field字段，查询field字段的值</span>
	<span class="token function">HSetNX</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> field <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>BoolCmd
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h3 id="_7-3-hget-hset等操作"><a href="#_7-3-hget-hset等操作" class="header-anchor">#</a> 7.3 HGet &amp; HSet等操作</h3> <blockquote><p>以下示例包含<code>HSet</code>、<code>HGet</code>、<code>HGetAll</code>、<code>HMGet</code>、<code>HMSet</code>、<code>HSetNX</code>等操作</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据key和field字段设置,field字段的值。 user_1 是hash key，username 是字段名, admin是字段值</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据key和field字段,查询field字段的值。user_1 是hash key，username是字段名</span>
	username<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HGet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span> <span class="token comment">//admin</span>
	<span class="token comment">//继续往user_1中添加字段password</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;password&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;abc123&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// HGetAll 一次性返回key=user_1的所有hash字段和值</span>
	data<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HGetAll</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// data是一个map类型，这里使用使用循环迭代输出</span>
	<span class="token keyword">for</span> field<span class="token punctuation">,</span> val <span class="token operator">:=</span> <span class="token keyword">range</span> data <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>field<span class="token punctuation">,</span> val<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 初始化hash数据的多个字段值</span>
	batchData <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
	batchData<span class="token punctuation">[</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&quot;test&quot;</span>
	batchData<span class="token punctuation">[</span><span class="token string">&quot;password&quot;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">123456</span>
	<span class="token comment">// 一次性保存多个hash字段值</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HMSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_2&quot;</span><span class="token punctuation">,</span> batchData<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//如果email字段不存在，则设置hash字段值</span>
	redisDb<span class="token punctuation">.</span><span class="token function">HSetNX</span><span class="token punctuation">(</span><span class="token string">&quot;user_2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;ourlang@foxmail.com&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// HMGet支持多个field字段名，意思是一次返回多个字段值</span>
	values<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HMGet</span><span class="token punctuation">(</span><span class="token string">&quot;user_2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;password&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// values是一个数组</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;user_2=&quot;</span><span class="token punctuation">,</span> values<span class="token punctuation">)</span> <span class="token comment">//user_2= [test 123456 ourlang@foxmail.com]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><h3 id="_7-4-hincrby-hincrbyfloat"><a href="#_7-4-hincrby-hincrbyfloat" class="header-anchor">#</a> 7.4 HIncrBy &amp; HIncrByFloat</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据key和field字段设置,field字段的值。 user_1 是hash key，username 是字段名, admin是字段值</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 累加count字段的值，一次性累加2， user_1为hash key</span>
	count<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HIncrBy</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;count&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span>
	<span class="token comment">// 累加score字段的值，一次性累加3.2， user_1为hash key</span>
	score<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HIncrByFloat</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;score&quot;</span><span class="token punctuation">,</span> <span class="token number">3.2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>score<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_7-5-hkeys-hlen示例"><a href="#_7-5-hkeys-hlen示例" class="header-anchor">#</a> 7.5 HKeys &amp; HLen示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据key和field字段设置,field字段的值。 user_1 是hash key，username 是字段名, admin是字段值</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 根据key返回所有字段名，keys是一个string数组</span>
	keys<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HKeys</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>keys<span class="token punctuation">)</span>

	<span class="token comment">//根据key，查询hash的字段数量</span>
	size<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HLen</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>size<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h3 id="_7-6-hdel-hexists示例"><a href="#_7-6-hdel-hexists示例" class="header-anchor">#</a> 7.6 HDel &amp; HExists示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据key和field字段设置,field字段的值。 user_1 是hash key，username 是字段名, admin是字段值</span>
	err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">HSet</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 删除一个字段id</span>
	redisDb<span class="token punctuation">.</span><span class="token function">HDel</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;score&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 删除多个字段</span>
	redisDb<span class="token punctuation">.</span><span class="token function">HDel</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;password&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;username&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//检测hash字段名是否存在,存在true 不存在false</span>
	exists<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">HExists</span><span class="token punctuation">(</span><span class="token string">&quot;user_1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> exists <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>exists<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="_8-有序集合-sorted-set"><a href="#_8-有序集合-sorted-set" class="header-anchor">#</a> 8 有序集合(sorted set)</h2> <blockquote><p><code>Redis</code>有序集合<code>(sorted set)</code>和集合一样也是<code>string</code>类型元素的集合,且不允许重复的成员,不同的是每个元素都会关联一个<code>double</code>类型的分数,这个分数主要用于集合元素排序。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// Z 表示已排序的集合成员</span>
<span class="token keyword">type</span> Z <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Score  <span class="token builtin">float64</span>  <span class="token comment">// 分数</span>
	Member <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// 元素名</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_8-1-有序集合的常用方法"><a href="#_8-1-有序集合的常用方法" class="header-anchor">#</a> 8.1 有序集合的常用方法</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Cmdable <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// 添加一个或者多个元素到集合，如果元素已经存在则更新分数</span>
    <span class="token function">ZAdd</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
	<span class="token function">ZAddNX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
	<span class="token function">ZAddXX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
	<span class="token function">ZAddCh</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
	<span class="token function">ZAddNXCh</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">// 添加一个或者多个元素到集合，如果元素已经存在则更新分数</span>
	<span class="token function">ZAddXXCh</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span>Z<span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//增加元素的分数</span>
	<span class="token function">ZIncr</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> member Z<span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
	<span class="token function">ZIncrNX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> member Z<span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
	<span class="token function">ZIncrXX</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> member Z<span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
    <span class="token comment">//增加元素的分数，增加的分数必须是float64类型</span>
	<span class="token function">ZIncrBy</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> increment <span class="token builtin">float64</span><span class="token punctuation">,</span> member <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
    <span class="token comment">// 存储增加分数的元素到destination集合</span>
	<span class="token function">ZInterStore</span><span class="token punctuation">(</span>destination <span class="token builtin">string</span><span class="token punctuation">,</span> store ZStore<span class="token punctuation">,</span> keys <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//返回集合元素个数</span>
    <span class="token function">ZCard</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//统计某个分数范围内的元素个数</span>
	<span class="token function">ZCount</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> min<span class="token punctuation">,</span> max <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//返回集合中某个索引范围的元素，根据分数从小到大排序</span>
	<span class="token function">ZRange</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//ZRevRange的结果是按分数从大到小排序。</span>
    <span class="token function">ZRevRange</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
	<span class="token comment">//根据分数范围返回集合元素，元素根据分数从小到大排序，支持分页。</span>
	<span class="token function">ZRangeByScore</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> opt ZRangeBy<span class="token punctuation">)</span> <span class="token operator">*</span>StringSliceCmd
    <span class="token comment">//根据分数范围返回集合元素，用法类似ZRangeByScore，区别是元素根据分数从大到小排序。</span>
    <span class="token function">ZRemRangeByScore</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> min<span class="token punctuation">,</span> max <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//用法跟ZRangeByScore一样，区别是除了返回集合元素，同时也返回元素对应的分数</span>
    <span class="token function">ZRangeWithScores</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>ZSliceCmd
    <span class="token comment">//根据元素名，查询集合元素在集合中的排名，从0开始算，集合元素按分数从小到大排序</span>
	<span class="token function">ZRank</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> member <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//ZRevRank的作用跟ZRank一样，区别是ZRevRank是按分数从大到小排序。</span>
    <span class="token function">ZRevRank</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> member <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd 
    <span class="token comment">//查询元素对应的分数</span>
	<span class="token function">ZScore</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> member <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">*</span>FloatCmd
    <span class="token comment">//删除集合元素</span>
	<span class="token function">ZRem</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> members <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
    <span class="token comment">//根据索引范围删除元素。从最低分到高分的（stop-start）个元素</span>
	<span class="token function">ZRemRangeByRank</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token operator">*</span>IntCmd
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><h3 id="_8-2-zadd-zincr示例"><a href="#_8-2-zadd-zincr示例" class="header-anchor">#</a> 8.2 ZAdd &amp; ZIncr示例</h3> <blockquote><p>以下示例包含<code>ZAdd</code>、<code>ZAddNX</code>、<code>ZAddXX</code>、<code>ZAddCh</code>、<code>ZAddNXCh</code>、<code>ZAddXXCh</code>、<code>ZIncr</code>、<code>ZIncrNX</code>、<code>ZIncrXX</code>、<code>ZIncrBy</code>、<code>ZInterStore</code></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>
	languages <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>redis<span class="token punctuation">.</span>Z<span class="token punctuation">{</span>
		<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">90.0</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;Golang&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">98.0</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;Java&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">95.0</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;Python&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">97.0</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;JavaScript&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">92.0</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;C/C++&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 添加一个或者多个元素到集合，如果元素已经存在则更新分数</span>
	num<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZAdd</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> languages<span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;zadd failed, err:%v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;ZAdd添加成功 %d 元素\n&quot;</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
	<span class="token comment">// 添加一个元素到集合</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZAdd</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> redis<span class="token punctuation">.</span>Z<span class="token punctuation">{</span>Score<span class="token punctuation">:</span> <span class="token number">87</span><span class="token punctuation">,</span> Member<span class="token punctuation">:</span> <span class="token string">&quot;Vue&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Err</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">//给元素Vue加上8分，最终vue得分95分</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZIncrBy</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token string">&quot;Vue&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 返回从0到-1位置的集合元素， 元素按分数从小到大排序 0到-1代表则返回全部数据</span>
	values<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZRange</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> val <span class="token operator">:=</span> <span class="token keyword">range</span> values <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><h3 id="_8-3-zcount-zcard示例"><a href="#_8-3-zcount-zcard示例" class="header-anchor">#</a> 8.3 ZCount &amp; ZCard示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>
	<span class="token comment">//返回集合元素的个数</span>
	size<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZCard</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>size<span class="token punctuation">)</span>
	<span class="token comment">//统计某个分数段内的元素个数，这里是查询的95&lt;分数&lt;100的元素个数</span>
	count<span class="token punctuation">,</span> err <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZCount</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token string">&quot;95&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;100&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_8-4-zrange等相关操作"><a href="#_8-4-zrange等相关操作" class="header-anchor">#</a> 8.4 ZRange等相关操作</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>
	<span class="token comment">// 返回从0到-1位置的集合元素， 元素按分数从小到大排序 0到-1代表则返回全部数据</span>
	<span class="token comment">//ZRevRange分数是从打到小排序，用法和ZRange一样</span>
	values<span class="token punctuation">,</span> err<span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZRange</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> val <span class="token operator">:=</span> <span class="token keyword">range</span> values <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 初始化查询条件， Offset和Count用于分页</span>
	op <span class="token operator">:=</span> redis<span class="token punctuation">.</span>ZRangeBy<span class="token punctuation">{</span>
		Min<span class="token punctuation">:</span><span class="token string">&quot;80&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 最小分数</span>
		Max<span class="token punctuation">:</span><span class="token string">&quot;100&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 最大分数</span>
		Offset<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token comment">// 类似sql的limit, 表示开始偏移量</span>
		Count<span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token comment">// 一次返回多少数据</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//根据分数范围返回集合元素，元素根据分数从小到大排序，支持分页。</span>
	values<span class="token punctuation">,</span> err <span class="token operator">=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZRangeByScore</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> op<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> val <span class="token operator">:=</span> <span class="token keyword">range</span> values <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="_8-5-zrank-zscore示例"><a href="#_8-5-zrank-zscore示例" class="header-anchor">#</a> 8.5 ZRank &amp; ZScore示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>
	<span class="token comment">// 查询集合元素Golang的分数</span>
	score<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZScore</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token string">&quot;Golang&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>score<span class="token punctuation">)</span>
	<span class="token comment">//根据元素名，查询集合元素在集合中的排名，从0开始算，集合元素按分数从小到大排序</span>
	rk<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">ZRank</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token string">&quot;Java&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>rk<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_8-6-删除集合元素"><a href="#_8-6-删除集合元素" class="header-anchor">#</a> 8.6 删除集合元素</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>

	<span class="token comment">// 删除集合中的元素Java</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZRem</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token string">&quot;Java&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">// 删除集合中的元素Golang和Vue</span>
	<span class="token comment">// 支持一次删除多个元素</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZRem</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token string">&quot;Golang&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Vue&quot;</span><span class="token punctuation">)</span>
	
	<span class="token comment">//根据索引范围删除元素</span>
	<span class="token comment">//集合元素按分数排序，从最低分到高分，删除第0个元素到第5个元素。</span>
	<span class="token comment">// 这里相当于删除最低分的几个元素</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZRemRangeByRank</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

	<span class="token comment">// 位置参数写成负数，代表从高分开始删除。</span>
	<span class="token comment">// 这个例子，删除最高分数的两个元素，-1代表最高分数的位置，-2第二高分，以此类推。</span>
	redisDb<span class="token punctuation">.</span><span class="token function">ZRemRangeByRank</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="_9-事务处理"><a href="#_9-事务处理" class="header-anchor">#</a> 9 事务处理</h2> <blockquote><p><code>redis</code>事务可以一次执行多个命令,事务中的命令要么全部被执行,要么全部都不执行。以下示例是以<code>Pipeline</code>的方式操作事务,接口结构如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> Pipeliner <span class="token keyword">interface</span> <span class="token punctuation">{</span>
	StatefulCmdable
	<span class="token function">Do</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">*</span>Cmd
	<span class="token function">Process</span><span class="token punctuation">(</span>cmd Cmder<span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token function">Discard</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token function">Exec</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Cmder<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_9-1-redis事务常用函数"><a href="#_9-1-redis事务常用函数" class="header-anchor">#</a> 9.1 redis事务常用函数</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//以Pipeline的方式操作事务</span>
<span class="token function">TxPipeline</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Pipeliner
Watch <span class="token operator">-</span> redis乐观锁支持
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_9-2-txpipeline示例"><a href="#_9-2-txpipeline示例" class="header-anchor">#</a> 9.2 TxPipeline示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//统计开发语言排行榜</span>
	zsetKey <span class="token operator">:=</span> <span class="token string">&quot;language_rank&quot;</span>
	<span class="token comment">// 开启一个TxPipeline事务</span>
	pipe <span class="token operator">:=</span> redisDb<span class="token punctuation">.</span><span class="token function">TxPipeline</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// 执行事务操作，可以通过pipe读写redis</span>
	incr <span class="token operator">:=</span> pipe<span class="token punctuation">.</span><span class="token function">Incr</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">)</span>
	pipe<span class="token punctuation">.</span><span class="token function">Expire</span><span class="token punctuation">(</span>zsetKey<span class="token punctuation">,</span> time<span class="token punctuation">.</span>Hour<span class="token punctuation">)</span>

	<span class="token comment">// 通过Exec函数提交redis事务</span>
	<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">=</span> pipe<span class="token punctuation">.</span><span class="token function">Exec</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// 提交事务后，我们可以查询事务操作的结果</span>
	<span class="token comment">// 前面执行Incr函数，在没有执行exec函数之前，实际上还没开始运行。</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>incr<span class="token punctuation">.</span><span class="token function">Val</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_9-3-watch"><a href="#_9-3-watch" class="header-anchor">#</a> 9.3 watch</h3> <blockquote><p><code>redis</code>乐观锁支持,可以通过<code>watch</code>监听一些<code>Key</code>,如果这些<code>key</code>的值没有被其他人改变的话,才可以提交事务。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">initClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token comment">//redis连接错误</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 定义一个回调函数，用于处理事务逻辑</span>
	fn <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span>tx <span class="token operator">*</span>redis<span class="token punctuation">.</span>Tx<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
		<span class="token comment">// 先查询下当前watch监听的key的值</span>
		v<span class="token punctuation">,</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token operator">&amp;&amp;</span> err <span class="token operator">!=</span> redis<span class="token punctuation">.</span>Nil <span class="token punctuation">{</span>
			<span class="token keyword">return</span> err
		<span class="token punctuation">}</span>

		<span class="token comment">// 这里可以处理业务</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>

		<span class="token comment">// 如果key的值没有改变的话，Pipelined函数才会调用成功</span>
		<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">=</span> tx<span class="token punctuation">.</span><span class="token function">Pipelined</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>pipe redis<span class="token punctuation">.</span>Pipeliner<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
			<span class="token comment">// 在这里给key设置最新值</span>
			pipe<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;new value&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
			<span class="token keyword">return</span> <span class="token boolean">nil</span>
		<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>

	<span class="token comment">// 使用Watch监听一些Key, 同时绑定一个回调函数fn, 监听Key后的逻辑写在fn这个回调函数里面</span>
	<span class="token comment">// 如果想监听多个key，可以这么写：client.Watch(fn, &quot;key1&quot;, &quot;key2&quot;, &quot;key3&quot;)</span>
	redisDb<span class="token punctuation">.</span><span class="token function">Watch</span><span class="token punctuation">(</span>fn<span class="token punctuation">,</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h2 id="_10-更多资料参考"><a href="#_10-更多资料参考" class="header-anchor">#</a> 10 更多资料参考</h2> <p><a href="/sql/nosql/Redis简介.html">Redis教程</a></p> <p><a href="https://godoc.org/github.com/go-redis/redis" target="_blank" rel="noopener noreferrer">go-redis文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/middleware/go-mysql.html" class="prev">
        Go语言操作Mysql
      </a></span> <span class="next"><a href="/go/middleware/go-swagger.html">
        go-swagger安装及使用
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/91.b8d1b7ea.js" defer></script>
  </body>
</html>
